<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃      
 * Date: 2015-12-21
 */

namespace Admin\Controller;
use Admin\Logic\GoodsLogic;

class ReportController extends BaseController{
	public $begin;
	public $end;
	public function _initialize(){
        parent::_initialize();
        $timegap = I('timegap');
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin = $gap[0];
			$end = $gap[1];
		}else{
			$lastweek = date('Y-m-d');//今天
			$begin = I('begin',$lastweek);
			$end =  I('end',date('Y-m-d'));
		}

		$this->begin = strtotime($begin);
		$this->end = strtotime($end)+86399;
		$this->assign('timegap',date('Y-m-d',$this->begin).' - '.date('Y-m-d',$this->end));
	}
	
	public function index(){
		$now = strtotime(date('Y-m-d'));
		$admin_id = session();
		$admin_id = $admin_id['admin_id'];
		$admin_role_f = M('admin')->field('role_id')->where("admin_id=$admin_id")->find();	
		if($admin_role_f['role_id'] ==1){
			$rolename = M('admin_role')->field('role_name,role_id')->where("role_id!=1")->select();

			$role = array();
			$roleids = array();
			foreach ($rolename as $key => $value) {

				$roleids[]=$value['role_id'];
			}


			$role_ids = implode(',', $roleids);
			$store_ids =  M('store')->field('store_id,roleid')->where("roleid in($role_ids)")->select();

			$diqus = array();
			foreach ($store_ids as $key => $v) {
				$diqus[$v['roleid']][] = $v['store_id'];
			}
			$today['today_amount'] = M('order')->where("add_time>$now AND pay_status=1  and order_status in(1,2,4) and roleid != 1")->sum('order_amount');//今日销售总额
			$today['today_order'] = M('order')->where("add_time>$now and pay_status=1  and roleid != 1")->count();//今日订单数
			$today['cancel_order'] = M('order')->where("add_time>$now AND order_status=3 and roleid != 1")->count();//今日取消订单
			$today['sign'] = round($today['today_amount']/$today['today_order'],2);
			$this->assign('today',$today);
			
			

			$sql = "SELECT COUNT(*) as tnum,sum(order_amount) as amount,roleid from  __PREFIX__order ";
			$sql .= " where roleid!=0 and add_time>$this->begin and add_time<$this->end AND (pay_status=1 or pay_code='weixin') and order_status in(1,2,4) and roleid != 1 group by roleid ";
			
			$res = M()->query($sql);//订单数,交易额
		
			$sql_order = "SELECT COUNT(*) as tnum,sum(order_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap from  __PREFIX__order ";
			$sql_order .= " where add_time>$this->begin and add_time<$this->end AND (pay_status=1 or pay_code='weixin') and order_status in(1,2,4) group by gap ";
			$res_order = M()->query($sql_order);//订单数,交易额

			if(count($roleids) != count($res)){
				foreach ($roleids as $v) {

					if(deep_in_array($v,$res,'roleid')) {
						continue;
					}else{
						$res[] = array('tnum'=>0,'amount'=>0,'roleid'=>$v['id']);
					}
				}
			}

			foreach ($res_order as $val){
				$arr[$val['gap']] = $val['tnum'];
				$brr[$val['gap']] = $val['amount'];
				$tnum += $val['tnum'];
				$tamount += $val['amount'];
			}

			foreach ($res as $key => $v) {
				$order_arr[]= $v['tnum'];
				$amount_arr[]= $v['amount'];
				$role= M('admin_role')->field('role_name')->where('role_id = '.$v['roleid'])->find();
				$roles[] = $role['role_name'];
			}

			for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
				$tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
				$tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
				$tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);					
				//$order_arr[] = $tmp_num;
				//$amount_arr[] = $tmp_amount;			
				$sign_arr[] = $tmp_sign;
				$date = date('Y-m-d',$i);
				$list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
				$day[] = $date;
			}
		}else{

			$admin_roleid = $admin_role_f['role_id'];
			 $today_amount= M('order')->where("add_time>$now AND pay_status=1  and order_status in(1,2,4) and roleid= $admin_roleid")->sum('order_amount');//今日销售总额
			$today_amount1 = M('order')->where("add_time>$now AND pay_status=1  and order_status in(6) and roleid= $admin_roleid")->sum('yajin');//今日销售总额
			$today['today_order'] = M('order')->where("add_time>$now and pay_status=1  and roleid= $admin_roleid")->count();//今日订单数
			$today['cancel_order'] = M('order')->where("add_time>$now AND order_status=3 and roleid= $admin_roleid")->count();//今日取消订单
			$today['sign'] = round($today['today_amount']/$today['today_order'],2);

			$today['today_amount']=$today_amount+$today_amount1;
			$this->assign('today',$today);
			
			

			$sql = "SELECT COUNT(*) as tnum,sum(order_amount) as amount,roleid from  __PREFIX__order ";
			$sql .= " where roleid!=0 and add_time>$this->begin and add_time<$this->end AND (pay_status=1 or pay_code='weixin') and order_status in(1,2,4) and roleid= $admin_roleid group by roleid ";
			
			$res = M()->query($sql);//订单数,交易额
		
			$sql_order = "SELECT COUNT(*) as tnum,sum(order_amount) as amount, FROM_UNIXTIME(add_time,'%Y-%m-%d') as gap from  __PREFIX__order ";
			$sql_order .= " where add_time>$this->begin and add_time<$this->end and roleid= $admin_roleid AND (pay_status=1 or pay_code='cod') and order_status in(1,2,4) group by gap ";
			$res_order = M()->query($sql_order);//订单数,交易额
			
			
			foreach ($res_order as $val){
				$arr[$val['gap']] = $val['tnum'];
				$brr[$val['gap']] = $val['amount'];
				$tnum += $val['tnum'];
				$tamount += $val['amount'];
			}

			if(empty($res))
			{
				$res[] = array('tnum'=>0,'amount'=>0,'roleid'=>$admin_roleid);
			}
			foreach ($res as $key => $v) {
				$order_arr[]= $v['tnum'];
				$amount_arr[]= $v['amount'];
				$role= M('admin_role')->field('role_name')->where('role_id = '.$v['roleid'])->find();
				$roles[] = $role['role_name'];
			}

			for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
				$tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
				$tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
				$tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);					
				//$order_arr[] = $tmp_num;
				//$amount_arr[] = $tmp_amount;			
				$sign_arr[] = $tmp_sign;
				$date = date('Y-m-d',$i);
				$list[] = array('day'=>$date,'order_num'=>$tmp_num,'amount'=>$tmp_amount,'sign'=>$tmp_sign,'end'=>date('Y-m-d',$i+24*60*60));
				$day[] = $date;
			}

		}
		$this->assign('list',$list);
		
		$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day,'rolename'=>$roles);
		
		$this->assign('result',json_encode($result));
		$this->display();
	}

	public function index_store_count(){
		$now = strtotime(date('Y-m-d'));
		$rolename = I('rolename');
		$store_id = M('admin_role')->field('role_id')->where(array("role_name"=>$rolename))->find();
		
		$store_infos = M('store')->field('store_id,store_name')->where("roleid = ".$store_id['role_id'])->select();
		$store_ids = array();
		$store_names = array();
		foreach ($store_infos as $key => $value) {
			$store_ids[] = $value['store_id'];
			$store_names[$value['store_id']] = $value['store_name'];
		}

		
		$store_ids = implode(',', $store_ids);

		$end =  I('end',date('Y-m-d'));
		

		$sql = "SELECT COUNT(*) as tnum,sum(order_amount) as amount,store_id from  __PREFIX__order ";
		$sql .= " where add_time>$this->begin and add_time<$this->end AND (pay_status=1 or pay_code='weixin') and order_status in(1,2,4) and roleid = ".$store_id['role_id']." group by store_id ";

		$res = M()->query($sql);//订单数,交易额


	    if(count($store_infos) != count($res)){
			foreach ($store_infos as $v) {
				if(deep_in_array($v['store_id'],$res,'store_id')) {
					continue;
				}else{
					$res[] = array('tnum'=>0,'amount'=>0,'store_id'=>$v['store_id']);
				}
			}
		}

		$store_names_t = array();
		foreach ($res as $key => $v) {
			$order_arr[]= $v['tnum'];
			$amount_arr[]= $v['amount'];
			$store_names_t[]= $store_names[$v['store_id']];

		}

		
		$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day,'rolename'=>$store_names_t);
		echo json_encode($result);
		die();
	}
/*
销售人员金额统计
*/

	public function index_seller_count(){
		$now = strtotime(date('Y-m-d'));
		$storename = I('storename');
		$store_id = M('store')->field('store_id')->where(array("store_name"=>$storename))->find();
		
		$seller_infos = M('seller')->field('user_id,seller_name')->where("store_id = ".$store_id['store_id']." and group_id !=0")->select();
		
		$seller_ids = array();
		$seller_names = array();
		foreach ($seller_infos as $key => $value) {
			$seller_ids[] = $value['user_id'];
			$seller_names[$value['user_id']] = $value['seller_name'];
		}

		$seller_ids = implode(',', $seller_ids);

	       
		$tguser = M('users')->field('user_id,nickname')->where("user_id in($seller_ids)")->select();
		

		$tguser_n = array();
		foreach($tguser as $key => $val)
		{
			$tguser_n[$val['user_id']]= $val['nickname'];
		}            
		
	
		$end =  I('end',date('Y-m-d'));		


		$sql = "SELECT COUNT(*) as tnum,sum(order_amount) as amount,tuiguangid from  __PREFIX__order ";
		$sql .= " where add_time>$this->begin and add_time<$this->end AND (pay_status=1 or pay_code='weixin') and order_status in(1,2,4) and tuiguangid in($seller_ids)  group by tuiguangid ";
		
		$res = M()->query($sql);//订单数,交易额
	


	    if(count($seller_infos) != count($res)){
			foreach ($seller_infos as $v) {
				if(deep_in_array($v['user_id'],$res,'tuiguangid')) {
					continue;
				}else{
					$res[] = array('tnum'=>0,'amount'=>0,'tuiguangid'=>$v['user_id']);
				}
			}
		}


		$seller_names_t = array();
		foreach ($res as $key => $v) {
			$order_arr[]= $v['tnum'];
			$amount_arr[]= $v['amount'];
			$seller_names_t[]= $tguser_n[$v['tuiguangid']];

		}

		$result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day,'rolename'=>$seller_names_t);
		echo json_encode($result);
		die();
	}

	public function saleTop(){
		$sql = "select goods_name,goods_sn,sum(goods_num) as sale_num,sum(goods_num*goods_price) as sale_amount from __PREFIX__order_goods ";
		$sql .=" where is_send = 1 group by goods_id order by sale_amount DESC limit 100";
		$res = M()->cache(true,3600)->query($sql);
		$this->assign('list',$res);
		$this->display();
	}
	
	public function userTop(){
		$p = I('p',1);
		$start = ($p-1)*20;		
		$mobile = I('mobile');
		$email = I('email');
		if($mobile){
			$where =  "and b.mobile='$mobile'";
		}
		if($email){
			$where = "and b.email='$email'";
		}
		$is_admin=session();
        $id=$is_admin['admin_id'];
        $roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();
        $roleid=$roleid['role_id'];

		$sql = "select count(a.order_id) as order_num,sum(a.order_amount) as amount,a.user_id,b.mobile,b.email from __PREFIX__order as a left join __PREFIX__users as b ";
		$sql .= " on a.user_id = b.user_id where a.add_time>$this->begin and a.add_time<$this->end and a.pay_status=1 and a.roleid=$roleid 
		$where group by user_id order by amount DESC limit $start,20";
		$res = M()->cache(true)->query($sql);
		$this->assign('list',$res);
		if(empty($where)){
			$count = M('order')->where("add_time>$this->begin and add_time<$this->end and pay_status=1")->group('user_id')->count();
			$Page = new \Think\Page($count,20);
			$show = $Page->show();
			$this->assign('page',$show);
		}
		$this->display();
	}
	
	public function saleList(){
		$p = I('p',1);
		$start = ($p-1)*20;
		$cat_id = I('cat_id',0);
		$brand_id = I('brand_id',0);
		$is_admin=session();
        $id=$is_admin['admin_id'];
        $roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();
        $roleid=$roleid['role_id'];
		$where = "where b.add_time>$this->begin and b.add_time<$this->end and b.order_status in(1,2,4) and b.roleid=$roleid ";
		if($cat_id>0){
			$where .= " and g.cat_id=$cat_id";
			$this->assign('cat_id',$cat_id);
		}
		if($brand_id>0){
			$where .= " and g.brand_id=$brand_id";
			$this->assign('brand_id',$brand_id);
		}
		$sql = "select a.*,b.order_sn,b.shipping_name,b.pay_name,b.add_time,b.tuiguangid,b.order_status from __PREFIX__order_goods as a left join __PREFIX__order as b on a.order_id=b.order_id ";
		$sql .= " left join __PREFIX__goods as g on a.goods_id = g.goods_id $where ";
		$sql .= "  order by add_time limit $start,20";

		$res = M()->query($sql);

		$this->assign('list',$res);
		
		$sql2 = "select count(*) as tnum from __PREFIX__order_goods as a left join __PREFIX__order as b on a.order_id=b.order_id ";
		$sql2 .= " left join __PREFIX__goods as g on a.goods_id = g.goods_id $where";
		$total = M()->query($sql2);
		$count =  $total[0]['tnum'];
		$Page = new \Think\Page($count,20);
		$show = $Page->show();
		$this->assign('page',$show);
		
        $GoodsLogic = new GoodsLogic();        
        $brandList = $GoodsLogic->getSortBrands();
        $categoryList = $GoodsLogic->getSortCategory();
        $this->assign('categoryList',$categoryList);
        $this->assign('brandList',$brandList);
		$this->display();
	}
	
	public function user(){
		$today = strtotime(date('Y-m-d'));
		$month = strtotime(date('Y-m-01'));
		$is_admin=session();
        $id=$is_admin['admin_id'];
        $roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();
        $roleid=$roleid['role_id'];
		$user['today'] = D('users')->where("reg_time>$today and roleid=$roleid")->count();//今日新增会员
		$user['month'] = D('users')->where("reg_time>$month and roleid=$roleid")->count();//本月新增会员
		$user['total'] = D('users')->where(" roleid=$roleid")->count();//会员总数
		$user['user_money'] = D('users')->where(" roleid=$roleid")->sum('user_money');//会员余额总额
		$res = M('order')->cache(true,10)->distinct(true)->field('user_id')->where(" roleid=$roleid")->select();
		$user['hasorder'] = count($res);
		$this->assign('user',$user);
		$sql = "SELECT COUNT(*) as num,FROM_UNIXTIME(reg_time,'%Y-%m-%d') as gap from __PREFIX__users where reg_time>$this->begin and reg_time<$this->end and roleid =$roleid group by gap";
		$new = M()->query($sql);//新增会员趋势		
		foreach ($new as $val){
			$arr[$val['gap']] = $val['num'];
		}
		
		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$brr[] = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
			$day[] = date('Y-m-d',$i);
		}		
		$result = array('data'=>$brr,'time'=>$day);
		$this->assign('result',json_encode($result));					
		$this->display();
	}
	
	//财务统计
	public function finance(){
		$sql = "SELECT sum(b.goods_num*b.member_goods_price) as goods_amount,sum(a.shipping_price) as shipping_amount,sum(b.goods_num*b.cost_price) as cost_price,";
		$sql .= "sum(a.coupon_price) as coupon_amount,FROM_UNIXTIME(a.add_time,'%Y-%m-%d') as gap from  __PREFIX__order a left join __PREFIX__order_goods b on a.order_id=b.order_id ";
		$sql .= " where a.add_time>$this->begin and a.add_time<$this->end AND a.pay_status=1 and a.shipping_status=1 and b.is_send=1 group by gap order by a.add_time";
		$res = M()->cache(true)->query($sql);//物流费,交易额,成本价
		
		foreach ($res as $val){
			$arr[$val['gap']] = $val['goods_amount'];
			$brr[$val['gap']] = $val['cost_price'];
			$crr[$val['gap']] = $val['shipping_amount'];
			$drr[$val['gap']] = $val['coupon_amount'];
		}
			
		for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
			$date = $day[] = date('Y-m-d',$i);
			$tmp_goods_amount = empty($arr[$date]) ? 0 : $arr[$date];
			$tmp_cost_amount = empty($brr[$date]) ? 0 : $brr[$date];
			$tmp_shipping_amount = empty($crr[$date]) ? 0 : $crr[$date];
			$tmp_coupon_amount = empty($drr[$date]) ? 0 : $drr[$date];
			
			$goods_arr[] = $tmp_goods_amount;
			$cost_arr[] = $tmp_cost_amount;
			$shipping_arr[] = $tmp_shipping_amount;
			$coupon_arr[] = $tmp_coupon_amount;
			$list[] = array('day'=>$date,'goods_amount'=>$tmp_goods_amount,'cost_amount'=>$tmp_cost_amount,
					'shipping_amount'=>$tmp_shipping_amount,'coupon_amount'=>$tmp_coupon_amount,'end'=>date('Y-m-d',$i+24*60*60));
		}
		$this->assign('list',$list);
		$result = array('goods_arr'=>$goods_arr,'cost_arr'=>$cost_arr,'shipping_arr'=>$shipping_arr,'coupon_arr'=>$coupon_arr,'time'=>$day);
		$this->assign('result',json_encode($result));
		$this->display();
	}
	public function doneday(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('admin_id');
		$roleid = M('admin')->field('role_id')->where("admin_id=$seller")->find();

		$roleid = $roleid['role_id'];

		$store_info = M('store')->field('store_id,store_name')->where("roleid=$roleid")->select();
		$total_list = array();
		foreach ($store_info as $key => $val) {
			$store_id=$val['store_id'];
			$sql="select count(id) as cishu  from ty_liliao where store_id=$store_id and  shijian >=$begin and shijian <=$end";
			$arr=M()->query($sql);
			/*每个人时间段内进馆人数*/
			$sql1="select count(user_id) as renshu from ty_users where seller_id=$store_id and  reg_time >=$begin and reg_time <=$end and tuixiao_id != 0";
			$arr1=M()->query($sql1);
	
			/*每个人时间段内回款数*/
			$sql2="select sum(total_amount) as total_amount from ty_order where store_id=$store_id and tuiguangid !=0 and  add_time >=$begin and add_time <=$end and order_status in(1,2,4)";
			$arr2=M()->query($sql2);
			$sql3="select sum(total_amount) as total_amount,sum(yajin) as yajin from ty_order where store_id=$store_id and  add_time >=$begin and add_time <=$end and tuiguangid !=0 and order_status=6";
			$arr3=M()->query($sql3);

			/*每个人时间段内办理疗卡张数*/
			$goods_info="select order_id,tuiguangid from ty_order where store_id=$store_id and  add_time >=$begin and add_time <=$end and tuiguangid !=0 and order_status in(1,2,4,6)";
	
			$arr4=M()->query($goods_info);
			$card_num = 0;
			foreach ($arr4 as $key => $v) {
				$card_num_n = M('order_goods')->field('goods_num')->where("order_id = ".$v['order_id'].' and (goods_name like "%理疗%" or goods_name like "%泥灸%")')->find();
				$card_num += $card_num_n['goods_num'];
			}
			
			$total_n = $arr2['0']['total_amount'];
		 	$total_b_in = $arr3['0']['total_amount'];
		 	$total_b_out = $arr3['0']['total_amount']-$arr3['0']['yajin'];

			
		
			$total_list[$val['store_id']] = array(
			'store_id'=>$val['store_id'],
			'store_name'=>$val['store_name'],
			'card_num'=>$card_num,
			'cishu'=>$arr['0']['cishu'],
			'renshu'=>$arr1['0']['renshu'],
			'b_in'=>$total_n+$total_b_in-$total_b_out,
			'b_out'=>$total_b_out,
		    );

			/*总计*/
			$t_card_num += $card_num;
			$t_cishu += $arr['0']['cishu'];
			$t_renshu += $arr1['0']['renshu'];
			$t_b_in += $total_n+$total_b_in-$total_b_out;
			$t_b_out += $total_b_out;
		}
		$total_list_t = array(
			't_card_num'=>$t_card_num,
			't_cishu'=>$t_cishu,
			't_renshu'=>$t_renshu,
			't_b_in'=>$t_b_in,
			't_b_out'=>$t_b_out,
		);
		
		$this->assign('total_list',$total_list);
		$this->assign('total_list_t',$total_list_t);
		$this->display();
	}
	

	public function doneday_store(){

		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}

		$store_id=I('store_id');

		$storeinfo = M('store')->field('store_name')->where("store_id=$store_id")->find();

		$list=M('seller')->field('ty_seller.seller_id,ty_users.user_id,ty_users.nickname,ty_seller_group.group_name')->join('ty_users on ty_seller.user_id=ty_users.user_id')->join('ty_seller_group on ty_seller.group_id =ty_seller_group.group_id')->where(array('ty_seller.store_id'=>$store_id,'ty_seller.is_admin'=>0))->order('ty_seller.group_id DESC')->select();
		$total_list = array();
		for($i=0;$i<count($list);$i++){
			$czy_id=$list[$i]['user_id'];

			//var_dump($czy_id);
			/*每个人时间段内服务次数*/
			$sql="select count(id) as cishu  from ty_liliao where czy_id=$czy_id and  shijian >=$begin and shijian <=$end";
			$arr=M()->query($sql);
			/*每个人时间段内进馆人数*/
			$sql1="select count(user_id) as renshu from ty_users where tuixiao_id=$czy_id and  reg_time >=$begin and reg_time <=$end";
			$arr1=M()->query($sql1);
			/*每个人时间段内回款数*/
			$sql2="select sum(total_amount) as total_amount from ty_order where tuiguangid=$czy_id and  add_time >=$begin and add_time <=$end and order_status in(1,2,4)";
			$arr2=M()->query($sql2);
			$sql3="select sum(total_amount) as total_amount,sum(yajin) as yajin from ty_order where tuiguangid=$czy_id and  add_time >=$begin and add_time <=$end and order_status=6";
			$arr3=M()->query($sql3);

			/*每个人时间段内办理疗卡张数*/
			$goods_info="select order_id from ty_order where tuiguangid=$czy_id and  add_time >=$begin and add_time <=$end and order_status in(1,2,4,6)";
			
			$arr4=M()->query($goods_info);
			$card_num = 0;
			foreach ($arr4 as $key => $v) {
				$card_num_n = M('order_goods')->field('goods_num')->where("order_id = ".$v['order_id'].' and (goods_name like "%理疗%" or goods_name like "%泥灸%")')->find();
				$card_num += $card_num_n['goods_num'];
			}
			
			$list[$i]['card_num']=$card_num;
			$list[$i]['cishu']=$arr['0']['cishu'];
			$list[$i]['renshu']=$arr1['0']['renshu'];
			$total_n = $arr2['0']['total_amount'];
		 	$total_b_in = $arr3['0']['total_amount'];
		 	$total_b_out = $arr3['0']['total_amount']-$arr3['0']['yajin'];
			$list[$i]['total_amount']=$total_n+$total_b_in-$total_b_out;
			$list[$i]['total_out']=$total_b_out;

			/*总计*/
			$t_card_num += $card_num;
			$t_cishu += $arr['0']['cishu'];
			$t_renshu += $list[$i]['renshu'];
			$t_b_in += $list[$i]['total_amount'];
			$t_b_out += $list[$i]['total_out'];
		}
		$total_list = array(
			't_card_num'=>$t_card_num,
			't_cishu'=>$t_cishu,
			't_renshu'=>$t_renshu,
			't_b_in'=>$t_b_in,
			't_b_out'=>$t_b_out,
		);
		$this->assign('total_list',$total_list);
		$this->assign('store_id',$store_id);

		$this->assign('storeinfo',$storeinfo);
		$this->assign('list',$list);
		$this->display();
	}


	public function countday(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('admin_id');
		$store_id=$seller['store_id'];
		$roleid = M('admin')->field('role_id')->where("admin_id=$seller")->find();

		$roleid = $roleid['role_id'];

		$store_info = M('store')->field('store_id,store_name')->where("roleid=$roleid")->order('store_id ASC')->select();

		$class = M('goods_category')->field('id,name')->where("level=3 and roleid=$roleid")->order('id ASC')->select();
		
		$class_id = array();
		$class_name = array();
		foreach ($class as $key => $v) {
			$class_id[] = $v['id'];
			$class_name[$v['id']] = $v['name'];
		}
		$class_ids = $class_id;
		$class_id = implode(',', $class_id);
		if($class_id==''){
			$class_id ='0';
		}
		$class_goods = M('goods')->field('goods_id,cat_id3,goods_name')->where("cat_id3 in ($class_id)")->order('cat_id3 ASC')->select();
	
		$class_goods_f = array();
		//获取分类的商品id
		foreach($class_goods as $v)
		{
			$class_goods_f[$v['cat_id3']][] = $v['goods_id'];
		}
		//根据分类的商品id查询总金额欠款等
		foreach ($store_info as $k => $n) {
			
		
		$order_detail = M('order')->field('user_id,consignee,tuiguangid,doctor_id,total_amount,yajin,order_id,pay_code,add_time')
		->where("store_id = ".$n['store_id']." and add_time >=$begin and add_time <=$end  and order_status in(1,2,4,6)")
		->select();
		$order_count_detail = array();

		foreach ($class_goods_f as $key => $v) {
			$vn = implode(',', $v);

			$classnum = 0;
			$class_total_fee = 0;
			$class_total_yajin = 0;
			$class_total_qk = 0;
			foreach ($order_detail as $ke => $va) {
				$order_goods_detail = M('order_goods')->field('goods_id,goods_num,goods_name')
				->where("order_id = ".$va['order_id']." and goods_id in($vn)")->find();
				$classname = $class_name[$key];
				if (!$order_goods_detail) {
					continue;
				}
				
				$classnum += $order_goods_detail['goods_num'];
				$class_total_fee +=  $va['total_amount'];
				
				if($va['yajin'] != "0.00") {
					$class_total_qk += $va['total_amount']-$va['yajin'];
					$class_total_yajin += $va['yajin'];
				}else{
					$class_total_qk +=0;
					$class_total_yajin += $va['total_amount'];
				}
			

			}
			
			$order_count_detail[$key] = array(
				'class_id' => $key,
				'classname' => $classname,
				'classnum' => $classnum,
				'class_total_fee' => $class_total_fee,
				'class_total_yajin' => $class_total_yajin,
				'class_total_qk' => $class_total_qk,
			);

			$total_fee[$key] += $class_total_fee;
			$total_num[$key] += $classnum;
			$total_yajin[$key] += $class_total_yajin;
			$total_qk[$key] += $class_total_qk;
			
		
		}

		$order_detail_all[$k] = $order_count_detail;
		
		
		}
		for ($i=0; $i < count($total_num); $i++) { 
			$total_list[$class_ids[$i]]['total_num'] = $total_num[$class_ids[$i]];
			$total_list[$class_ids[$i]]['total_fee'] = $total_fee[$class_ids[$i]];
			$total_list[$class_ids[$i]]['total_yajin'] = $total_yajin[$class_ids[$i]];
			$total_list[$class_ids[$i]]['total_qk'] = $total_qk[$class_ids[$i]];
		}
		
	
		$this->assign('total_list',$total_list);
		$this->assign('order_count_detail',$order_detail_all);
		$this->assign('storeinfo',$store_info);
		$this->assign('class_name',$class_name);

		$this->display();
	}	


	public function store_countday(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$store_id=I('store_id');

		$storeinfo = M('store')->field('store_id,store_name')->where("store_id=$store_id")->find();
		
		$class = M('goods_category')->field('id,name')->where("level=3")->select();
		$class_id = array();
		$class_name = array();
		foreach ($class as $key => $v) {
			$class_id[] = $v['id'];
			$class_name[$v['id']] = $v['name'];
		}
		$class_id = implode(',', $class_id);
		
		$class_goods = M('goods')->field('goods_id,cat_id3,goods_name')->where("cat_id3 in ($class_id)")->select();
	
		$class_goods_f = array();
		//获取分类的商品id
		foreach($class_goods as $v)
		{
			$class_goods_f[$v['cat_id3']][] = $v['goods_id'];
		}
		//根据分类的商品id查询总金额欠款等
		$order_detail = M('order')->field('user_id,consignee,tuiguangid,doctor_id,total_amount,yajin,order_id,pay_code,add_time')
		->where("store_id = $store_id and add_time >=$begin and add_time <=$end  and order_status in(1,2,4,6)")
		->select();
		$order_count_detail = array();

		foreach ($class_goods_f as $key => $v) {
			$vn = implode(',', $v);

			$classnum = 0;
			$class_total_fee = 0;
			$class_total_yajin = 0;
			$class_total_qk = 0;
			foreach ($order_detail as $ke => $va) {
				$order_goods_detail = M('order_goods')->field('goods_id,goods_num,goods_name')
				->where("order_id = ".$va['order_id']." and goods_id in($vn)")->find();
				$classname = $class_name[$key];
				if (!$order_goods_detail) {
					continue;
				}
				
				$classnum += $order_goods_detail['goods_num'];
				$class_total_fee +=  $va['total_amount'];
				
				if($va['yajin'] != "0.00") {
					$class_total_qk += $va['total_amount']-$va['yajin'];
					$class_total_yajin += $va['yajin'];
				}else{
					$class_total_qk +=0;
					$class_total_yajin += $va['total_amount'];
				}
				$class_total_hk .= "[".$va['pay_code']."]";

			}
			
			$order_count_detail[$key] = array(
				'class_id' => $key,
				'classname' => $classname,
				'classnum' => $classnum,
				'class_total_fee' => $class_total_fee,
				'class_total_yajin' => $class_total_yajin,
				'class_total_qk' => $class_total_qk,
				'class_total_hk' => $class_total_hk,
			);
			$total_fee += $class_total_fee;
			$total_num += $classnum;
			$total_yajin += $class_total_yajin;
			$total_qk += $class_total_qk;
		}

		$total_list = array(
				'total_num' => $total_num,
				'total_fee' => $total_fee,
				'total_yajin' => $total_yajin,
				'total_qk' => $total_qk,
			);
		$this->assign('total_list',$total_list);
		$this->assign('order_count_detail',$order_count_detail);
		$this->assign('storeinfo',$storeinfo);

		$this->display();
	}	
	

	public function store_countday_detail(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$class_id = I('class_id');

		$store_id=I('store_id');
		$storeinfo = M('store')->field('store_name,store_id')->where("store_id=$store_id")->find();
		$sellerinfo = M('users')->field('nickname,user_id')->where("seller_id=$store_id and group_id in(5,7)")->select();
		$sellerinfos = array();
		foreach ($sellerinfo as $key => $value) {
			$sellerinfos[$value['user_id']] = $value['nickname'];
		}
		$class_goods = M('goods')->field('goods_id,cat_id3,goods_name')->where("cat_id3 =$class_id")->select();
	
		$class_goods_f = array();
		//获取分类的商品id
		foreach($class_goods as $v)
		{
			$class_goods_f[] = $v['goods_id'];
		}
		$goods_id_t = implode(',', $class_goods_f);
		//根据分类的商品id查询总金额欠款等
		$order_detail = M('order')->field('user_id,consignee,tuiguangid,doctor_id,total_amount,yajin,order_id,pay_code,add_time')
		->where("store_id = $store_id and add_time >=$begin and add_time <=$end  and order_status in(1,2,4,6)")
		->select();
		$order_count_detail = array();

			

			foreach ($order_detail as $ke => $va) {
				$classnum = 0;
			$class_total_fee = 0;
			$class_total_yajin = 0;
			$class_total_qk = 0;
				$order_goods_detail = M('order_goods')->field('goods_id,goods_num,goods_name')
				->where("order_id = ".$va['order_id']." and goods_id in($goods_id_t)")->find();
				
				$classname = $order_goods_detail['goods_name'];
				if (!$order_goods_detail) {
					continue;
				}
				
				$classnum += $order_goods_detail['goods_num'];
				$class_total_fee +=  $va['total_amount'];
				
				if($va['yajin'] != "0.00") {
					$class_total_qk += $va['total_amount']-$va['yajin'];
					$class_total_yajin += $va['yajin'];
				}else{
					$class_total_qk +=0;
					$class_total_yajin += $va['total_amount'];
				}
				$class_total_hk .= "[".$va['pay_code']."]";
				$order_count_detail[] = array(
					'class_id' => $class_id,
					'classname' => $classname,
					'classnum' => $classnum,
					'class_total_fee' => $class_total_fee,
					'class_total_yajin' => $class_total_yajin,
					'class_total_qk' => $class_total_qk,
					'class_total_hk' => $class_total_hk,
					'lls' => $va['tuiguangid'],
				);
				$total_fee += $class_total_fee;
				$total_num += $classnum;
				$total_yajin += $class_total_yajin;
				$total_qk += $class_total_qk;
			}
			
			
			
	
		$total_list = array(
				'total_num' => $total_num,
				'total_fee' => $total_fee,
				'total_yajin' => $total_yajin,
				'total_qk' => $total_qk,
			);
		$this->assign('total_list',$total_list);
		$this->assign('order_count_detail',$order_count_detail);
		$this->assign('storeinfo',$storeinfo);
		$this->assign('class_id',$class_id);
		$this->assign('sellerinfos',$sellerinfos);

		$this->display();
	}	

    public function export_excel()
    {
        //搜索条件
        $timegap=I('timegap');
		
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}

        $seller =session('admin_id');
		$store_id=$seller['store_id'];
		$roleid = M('admin')->field('role_id')->where("admin_id=$seller")->find();

		$roleid = $roleid['role_id'];

		$store_info = M('store')->field('store_id,store_name')->where("roleid=$roleid")->order('store_id ASC')->select();

		$class = M('goods_category')->field('id,name')->where("level=3")->order('id ASC')->select();
		$class_id = array();
		$class_name = array();
		foreach ($class as $key => $v) {
			$class_id[] = $v['id'];
			$class_name[$v['id']] = $v['name'];
		}
		$class_ids = $class_id;
		$class_id = implode(',', $class_id);
		
		$class_goods = M('goods')->field('goods_id,cat_id3,goods_name')->where("cat_id3 in ($class_id)")->order('cat_id3 ASC')->select();
	
		$class_goods_f = array();
		//获取分类的商品id
		foreach($class_goods as $v)
		{
			$class_goods_f[$v['cat_id3']][] = $v['goods_id'];
		}
		//根据分类的商品id查询总金额欠款等
		foreach ($store_info as $k => $n) {
			
		
		$order_detail = M('order')->field('user_id,consignee,tuiguangid,doctor_id,total_amount,yajin,order_id,pay_code,add_time')
		->where("store_id = ".$n['store_id']." and add_time >=$begin and add_time <=$end  and order_status in(1,2,4,6)")
		->select();
		$order_count_detail = array();

		foreach ($class_goods_f as $key => $v) {
			$vn = implode(',', $v);

			$classnum = 0;
			$class_total_fee = 0;
			$class_total_yajin = 0;
			$class_total_qk = 0;
			foreach ($order_detail as $ke => $va) {
				$order_goods_detail = M('order_goods')->field('goods_id,goods_num,goods_name')
				->where("order_id = ".$va['order_id']." and goods_id in($vn)")->find();
				$classname = $class_name[$key];
				if (!$order_goods_detail) {
					continue;
				}
				
				$classnum += $order_goods_detail['goods_num'];
				$class_total_fee +=  $va['total_amount'];
				
				if($va['yajin'] != "0.00") {
					$class_total_qk += $va['total_amount']-$va['yajin'];
					$class_total_yajin += $va['yajin'];
				}else{
					$class_total_qk +=0;
					$class_total_yajin += $va['total_amount'];
				}
			

			}
			
			$order_count_detail[$key] = array(
				'class_id' => $key,
				'classname' => $classname,
				'classnum' => $classnum,
				'class_total_fee' => $class_total_fee,
				'class_total_yajin' => $class_total_yajin,
				'class_total_qk' => $class_total_qk,
			);

			$total_fee[$key] += $class_total_fee;
			$total_num[$key] += $classnum;
			$total_yajin[$key] += $class_total_yajin;
			$total_qk[$key] += $class_total_qk;
			
		
		}

		$order_detail_all[$k] = $order_count_detail;
		
		
		}
		for ($i=0; $i < count($total_num); $i++) { 
			$total_list[$class_ids[$i]]['total_num'] = $total_num[$class_ids[$i]];
			$total_list[$class_ids[$i]]['total_fee'] = $total_fee[$class_ids[$i]];
			$total_list[$class_ids[$i]]['total_yajin'] = $total_yajin[$class_ids[$i]];
			$total_list[$class_ids[$i]]['total_qk'] = $total_qk[$class_ids[$i]];
		}
		

        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">店名</td>';
        foreach ($class_name as $key => $class) {
        	 $strTable .= '<td style="text-align:center;font-size:12px;" width="*">'. $class.'数量</td>';
        	 $strTable .= '<td style="text-align:center;font-size:12px;" width="*">'. $class.'金额</td>';
        	 $strTable .= '<td style="text-align:center;font-size:12px;" width="*">'. $class.'押金</td>';
        	 $strTable .= '<td style="text-align:center;font-size:12px;" width="*">'. $class.'欠款</td>';
        }
       
        $strTable .= '</tr>';
      
        foreach($order_detail_all as $k=>$v){
        	$strTable .= '<tr>';
        	$strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$store_info[$k]['store_name'].'</td>';
	            
        	
        	foreach($v as $kl=>$n){
	            
	            $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$n['classnum'].'</td>';
	            $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$n['class_total_fee'].'</td>';
	            $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$n['class_total_yajin'].'</td>';
	            $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$n['class_total_qk'].'</td>';
	          
        	}
        	$strTable .= '</tr>';
        }
      
		$strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;总计</td>';
        foreach ($total_list as $key => $v) {
	        $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$v['total_num'].'</td>';
	        $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$v['total_fee'].'</td>';
	        $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$v['total_yajin'].'</td>';
	        $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$v['total_qk'].'</td>';
	    }
	    $strTable .= '</tr>';    
        $strTable .='</table>';
        
        downloadExcel($strTable,'report');
        exit();
    }
    public function ysbiao(){
		$timegap=$_POST['timegap'];
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('admin_id');
		$roleid = M('admin')->field('role_id')->where("admin_id=$seller")->find();
		$roleid = $roleid['role_id'];
		$where=" store_id=0 and  is_admin=0  and group_id=8  and roleid=$roleid ";
        $tui=M('seller')->field('user_id')->where($where)->order('group_id desc')->select();

        foreach($tui as &$val){
			$store_id=$val['user_id'];
            $order1=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%膏方%') and 
            	ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
            $order2=M('order')->field('sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%中草药%') and ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$order3=M('order')->field('sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%中成药%') and ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$order4=M('order')->field('sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%小针刀%') and ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$order5=M('order')->field('sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%针灸%') and ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$order6=M('order')->field('sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%胶囊%') and ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$order7=M('order')->field('sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.doctor_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%膏方%') and 
            	ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$name=M('users')->field('nickname')->where("user_id=$store_id")->find();
			$val['order1']=$order1;
			$val['order2']=$order2;
			$val['order3']=$order3;
			$val['order4']=$order4;
			$val['order5']=$order5;
			$val['order6']=$order6;
			$val['order7']=$order7;
			$val['name']=$name;

		}
		$liliaozl1=0;
       	$liliaozl2=0;
       	$liliaozl3=0;
       	$liliaozl4=0;
       	$liliaozl5=0;
       	$liliaozl6=0;
       	$liliaozl7=0;
        foreach($tui as $item){  
            $liliaozl1 +=  $item['order1']['shuliang'];
            $liliaozl2 +=  $item['order2']['zongjia'];
            $liliaozl3 +=  $item['order3']['zongjia'];
            $liliaozl4 +=  $item['order4']['zongjia'];
            $liliaozl5 +=  $item['order5']['zongjia'];
            $liliaozl6 +=  $item['order6']['zongjia'];
            $liliaozl7 +=  $item['order7']['zongjia'];     
        } 

        $total_list = array(
			'liliaozl1'=>$liliaozl1,
			'liliaozl2'=>$liliaozl2,
			'liliaozl3'=>$liliaozl3,
			'liliaozl4'=>$liliaozl4,
			'liliaozl5'=>$liliaozl5,
			'liliaozl6'=>$liliaozl6,
			'liliaozl7'=>$liliaozl7,
		);
		$this->assign('total_list',$total_list);
		$this->assign('list',$tui);
        $this->display();
	}
	public function biao(){
		$timegap=$_POST['timegap'];
		
		if($timegap){
			$gap = explode(' - ', $timegap);

			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399; 
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('admin_id');
		$roleid = M('admin')->field('role_id')->where("admin_id=$seller")->find();
		$roleid = $roleid['role_id'];

		$store_info = M('store')->field('store_id,store_name')->where("roleid=$roleid")->select();
		
		foreach($store_info as &$val){
			$store_id=$val['store_id'];
			$order=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ,sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%理疗%' or ty_order_goods.goods_name like '%泥灸%') and ty_order.add_time >=$begin and ty_order.add_time <=$end  ")->find();
            $order1=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ,sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%膏方%') and 
            	ty_order.add_time >=$begin and ty_order.add_time <=$end ")->find();
			$val['order']=$order;
			$val['order1']=$order1;

		}
		$liliaozl=0;
        $liliaozj=0;
        $gaofangzl=0;
        $gaofangzj=0;
        foreach($store_info as $item){  
            $liliaozl +=  $item['order']['shuliang'];
            $liliaozj +=  $item['order']['zongjia'];
            $gaofangzl +=  $item['order1']['shuliang'];
            $gaofangzj +=  $item['order1']['zongjia'];
                
        } 
        $total_list = array(
			'liliaozl'=>$liliaozl,
			'liliaozj'=>$liliaozj,
			'gaofangzl'=>$gaofangzl,
			'gaofangzj'=>$gaofangzj,
			
		);
		$this->assign('total_list',$total_list);
		$this->assign('list',$store_info);
		$this->display();
	}
	public function biao_store(){
		$timegap=$_POST['timegap'];
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$store_id=I('store_id');
		$where="store_id=$store_id and is_admin=0 and group_id=6 ";
        $tui=M('seller')->field('seller_id')->where($where)->order('group_id desc')->select();
        foreach($tui as $val){
           	$seller_id=$val['seller_id'];
           	$where="store_id=$store_id and shangji_id=$seller_id  and is_admin=0   ";
           	$sql=M('seller')->field('seller_id')->where($where)->order('group_id desc')->select();
           	foreach($sql as &$v){
           		$where="store_id=$store_id and (seller_id =$v[seller_id]  or shangji_id=$v[seller_id]) and is_admin=0 ";
                $sql2=M('seller')->field('user_id,group_id')->where($where)->order('group_id desc')->select();
                $liliaozl=0;
			    $liliaozj=0;
			    $gaofangzl=0;
			    $gaofangzj=0;
                foreach($sql2 as &$v1){
                	
		            $order=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ,sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where("ty_order.tuiguangid=$v1[user_id] and ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%理疗%' or ty_order_goods.goods_name like '%泥灸%') and ty_order.add_time >=$begin and ty_order.add_time <=$end")->find();
		            $order1=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ,sum(ty_order.total_amount) as zongjia')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where("ty_order.tuiguangid=$v1[user_id] and ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%膏方%') and ty_order.add_time >=$begin and ty_order.add_time <=$end")->find();
		            
		            $name=M('users')->field('nickname')->where("user_id=$v1[user_id]")->find();
		            
		                $v1['order']=$order;
		                $v1['order1']=$order1;
		                $v1['name']=$name; 
		                
		            $liliaozl+=$order['shuliang'];
                    $liliaozj+=$order['zongjia'];

                   	$gaofangzl+=$order1['shuliang'];
                    $gaofangzj+=$order1['zongjia'];
                }
              
            	$v['tj']=$sql2;
            	$v['liliaozl']=$liliaozl;
		    	$v['liliaozj']=$liliaozj;
		    	$v['gaofangzl']=$gaofangzl;
		    	$v['gaofangzj']=$gaofangzj;
         	}
    	} 
    	$kong=array(
        	'tongji'=>$sql,
    	); 
    	$liliaozl2=0;
		$liliaozj2=0;
		$gaofangzl2=0;
		$gaofangzj2=0;
		foreach($kong as $item){
			
			foreach($item as $v3){
			
	           	$liliaozl2 +=  $v3['liliaozl'];
		        $liliaozj2 +=  $v3['liliaozj'];
		        $gaofangzl2 +=  $v3['gaofangzl'];
		        $gaofangzj2 +=  $v3['gaofangzj'];
	        }
	    }
	    
	    $total_list = array(
			'liliaozl'=>$liliaozl2,
			'liliaozj'=>$liliaozj2,
			'gaofangzl'=>$gaofangzl2,
			'gaofangzj'=>$gaofangzj2,
		);

	    $this->assign('store_id',$store_id);
		$this->assign('total_list',$total_list); 	   
    	$this->assign('list',$kong);
		$this->display();
	}
	public function xiaoshou(){
		header("Content-type: text/html; charset=utf-8");
		$timegap=$_POST['timegap'];
		if($timegap){
			$gap = explode(' - ', $timegap);
			$begin =strtotime($gap[0]);
			$end = strtotime($gap[1])+86399;
		}else{

			$lastweek = date('Y-m-d');//30天前
			
			$begin = strtotime(I('begin',$lastweek));
		
			$end = time();
			
		}
		$seller =session('admin_id');
		$roleid = M('admin')->field('role_id')->where("admin_id=$seller")->find();

		$roleid = $roleid['role_id'];
		$store=M('store')->field('store_id,store_name')->where(array('roleid'=>$roleid))->select();
		
		foreach($store as $k => & $val){
			$store_id=$val['store_id'];
			$order=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ,sum(ty_order.total_amount) as zongjia,ty_order.tuiguangid')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%理疗%' or ty_order_goods.goods_name like '%泥灸%') and ty_order.add_time >=$begin and ty_order.add_time <=$end  ")->group('tuiguangid')->order('sum(ty_order.total_amount) desc')->select();
            $order1=M('order')->field('sum(ty_order_goods.goods_num) as shuliang ,sum(ty_order.total_amount) as zongjia, ty_order.tuiguangid')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%膏方%') and ty_order.add_time >=$begin and ty_order.add_time <=$end ")->group('tuiguangid')->order('sum(ty_order.total_amount) desc')->select();
   			
   			$arr=$order['0'];
   			$name=M('users')->field('nickname')->where(array('user_id'=>$arr['tuiguangid']))->find();
   			$zs=0;
   			$zj=0;
   			foreach($order as $v1){
   				$zs+=$v1['shuliang'];
   				$zj+=$v1['zongjia'];
   			}
   			$arr1=$order1['0'];
   			$name1=M('users')->field('nickname')->where(array('user_id'=>$arr1['tuiguangid']))->find();
   			$zs1=0;
   			$zj1=0;
   			foreach($order1 as $v2){
   				$zs1+=$v2['shuliang'];
   				$zj1+=$v2['zongjia'];
   			}
   			$val['zs']=$zs;
   			$val['zj']=$zj;
   			$val['name']=$name['nickname'];
   			$val['sl']=$arr['shuliang'];
   			$val['zs1']=$zs1;
   			$val['zj1']=$zj1;
   			$val['name1']=$name1['nickname'];
   			$val['sl1']=$arr1['shuliang'];
   			$seller=M('seller')->field('seller_id')->where("store_id=$store_id and group_id=7 and is_admin=0")->select();

   			$liliao=array();
   			$gaofang=array();
   			foreach($seller as &$v1){
   				$seller_id=$v1['seller_id'];
   				$user_id=M('seller')->field('user_id')->where("(seller_id =$seller_id or shangji_id=$seller_id) and is_admin=0 ")->select();
   				$tuiguangid=array();
   				foreach($user_id as $v2){
   					$tuiguangid[]=$v2['user_id'];
   				}
   				$tui=implode(',', $tuiguangid);
   				$order2=M('order')->field('sum(ty_order_goods.goods_num) as shuliang,tuiguangid')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and (ty_order_goods.goods_name like '%理疗%' or ty_order_goods.goods_name like '%泥灸%') and ty_order.add_time >=$begin and ty_order.add_time <=$end and tuiguangid in ($tui) ")->group('ty_order.store_id')->order('shuliang desc')->find();
   			  	$v1['order2']=$order2['shuliang'];
   			  	$v1['user_id2']=$order2['tuiguangid'];
   				$liliao[]=$order2['shuliang'];
   				$order3=M('order')->field('sum(ty_order_goods.goods_num) as shuliang,tuiguangid')->join('ty_order_goods on ty_order.order_id =ty_order_goods.order_id')->where(" ty_order.store_id =$store_id and ty_order.order_status in(1,2,4,6) and ty_order_goods.goods_name like '%膏方%' and ty_order.add_time >=$begin and ty_order.add_time <=$end and tuiguangid in ($tui) ")->group('ty_order.store_id')->order('shuliang desc')->find();
   			  	$v1['order3']=$order3['shuliang'];
   			  	$v1['user_id3']=$order3['tuiguangid'];
   				$gaofang[]=$order3['shuliang'];

   			}
   		
   			/*$a=max($liliao);
   			$b=max($gaofang);*/
   			
   			
   			foreach($seller as $v2){
   				if($v2['order2']==$a){
   					$liliaozl=$v2;
   				}
   			}
   			
   			$user_id=$liliaozl['user_id2'];
   			if($user_id==''){
   				$user_id='0';
   			}
   			$user=M('users')->field('nickname')->where("user_id=$user_id")->find();
   			$gaofangzl=array();
   			foreach($seller as $v3){
   				if($v3['order3']==$b){
   					$gaofangzl=$v3;
   				}
   			}
   			
   			$user_id1=$gaofangzl['user_id3'];
   			if($user_id1==''){
   				$user_id1='0';
   			}
   			$user1=M('users')->field('nickname')->where("user_id=$user_id1")->find();
   			$val['liliaozl']=$liliaozl['order2'];
   			$val['user']=$user['nickname'];
   			$val['gaofangzl']=$gaofangzl['order3'];
   			$val['user1']=$user1['nickname'];
		}

		$zsl=0;
		$zjg=0;
		$zsl1=0;
		$zjg1=0;
		//var_dump($store);
		foreach($store as $v){
			$zsl+=$v['zs'];
			$zjg+=$v['zj'];
			$zsl1+=$v['zs1'];
			$zjg1+=$v['zj1'];
		}
		$total_list = array(
			'zsl'=>$zsl,
			'zjg'=>$zjg,
			'zsl1'=>$zsl1,
			'zjg1'=>$zjg1,
		);
		//var_dump($store);
		$this->assign('total_list',$total_list);
		$this->assign('store',$store);
		$this->display();
	}

}